﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        int i=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        datasever data = new datasever();
        conn = data.getconnect();
        if (Request.QueryString["action"] != null)
        {
            if (Request.QueryString["action"] == "delete")
            {
                xoaNhanVien(Request.QueryString["manv"].ToString());
            }
            if (Request.QueryString["action"] == "edit")
            {
                Response.Redirect("editnv.aspx?manv="+Request.QueryString["manv"]);
            }
            if (Request.QueryString["action"] == "add")
            {
                Response.Redirect("editnv.aspx?action=add");
            }
        }
        gvTaiKhoang.AllowPaging = true;
        gvTaiKhoang.PageSize = 4;
        if (IsPostBack==false)
        {
            load_data();
        }
        //datal.DataSource = ds.Tables[0];
        //datal.RepeatColumns = 2;
        //datal.DataBind();
       

    }
    public void load_data()
    {
        cmd = new SqlCommand("getallnhanvien", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        ds = new DataSet();
        da = new SqlDataAdapter(cmd);
        da.Fill(ds, "nhanvien");
        //gvTaiKhoang.Columns[0].HeaderText = "STT";
       // gvTaiKhoang.Columns[1].HeaderText = "Delete";
        gvTaiKhoang.DataSource = ds.Tables[0];
        gvTaiKhoang.DataBind();
        gvTaiKhoang.HeaderStyle.BackColor = Color.Red;
        rep.DataSource = ds.Tables[0];
        rep.DataBind();
    }

    public void xoaNhanVien(String manv){
        String sql = "xoanhanvien";
        cmd = new SqlCommand(sql, conn);
        cmd.CommandType=CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@manv",Request.QueryString["manv"].ToString()));    
        cmd.ExecuteNonQuery();
    }
    protected void btsearch_Click(object sender, EventArgs e)
    {

        cmd = new SqlCommand("searchnhanvien", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@tennv", txtsearch.Text));
        ds = new DataSet();
        da = new SqlDataAdapter(cmd);
        da.Fill(ds, "nhanvien");
        //gvTaiKhoang.Columns[4].HeaderText = "STT";
        //gvTaiKhoang.Columns[1].HeaderText = "Delete";
        gvTaiKhoang.DataSource = ds.Tables[0];
        gvTaiKhoang.DataBind();
    }
    protected void gvTaiKhoang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTaiKhoang.PageIndex = e.NewPageIndex;
        int trang_thu = e.NewPageIndex;      //the hien trang thu may
        int so_dong = gvTaiKhoang.PageSize;       //moi trang co bao nhieu dong
        i = trang_thu * so_dong;
        load_data();
            
    }
    public String getstt(){
        i++;
        return i.ToString();
    }
    protected void btall_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gv in gvTaiKhoang.Rows)
        {
            CheckBox ckb = (CheckBox)gv.FindControl("ckxoa");
            ckb.Checked = true;
        }
    }
    protected void btxoa_Click(object sender, EventArgs e)
    {
        datasever data = new datasever();
        conn = data.getconnect();
       
        foreach (GridViewRow gv in gvTaiKhoang.Rows)
        {           
            CheckBox ckb = (CheckBox)gv.FindControl("ckxoa");
            if (ckb.Checked == true)
            {
                cmd = new SqlCommand("xoanhanvien", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                Response.Write(gv.Cells[4].Text);
                cmd.Parameters.Add(new SqlParameter("@manv", gv.Cells[4].Text));
                cmd.ExecuteNonQuery();
              
            }
        }
        conn.Close();
        Response.Redirect("Default.aspx");
    }
}